package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.BookBorrow;
import com.example.demo.entity.BookInfo;
import com.example.demo.mapper.BookInfoMapper;
import com.example.demo.service.BookBorrowService;
import com.example.demo.service.BookInfoService;
import com.example.demo.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/info")
public class BookInfoController {

    @Autowired
    private BookInfoService bookInfoService;

    @Autowired
    private BookInfoMapper bookInfoMapper;

    @Autowired
    private BookBorrowService bookBorrowService;



    /**
     * 展示所有书籍
     * @param model
     * @param request
     * @return
     */
    @GetMapping()
    public String list(Model model, HttpServletRequest request){

        //登录拦截
        if (request.getSession().getAttribute("username")==null){
            model.addAttribute("msg","未登录");
            return "login";
        }


        //2表查询 书类型
        List<BookInfo> list = bookInfoService.infoList();


        model.addAttribute("books",list);
        return "/static/book/info.html";
    }


    @GetMapping("/listBookInfo")
    public AjaxResult listBookInfo() {

        List<BookInfo> info = bookInfoService.infoList();

        return AjaxResult.success(info);
    }

    @GetMapping("/getBookInfo")
    public AjaxResult getBookInfo(Long id) {

        BookInfo info = bookInfoMapper.selectByPrimaryKey(id);

        return AjaxResult.success(info);
    }


    /**
     * 添加
     * @param bookInfo
     * @return
     */
    @PostMapping("/addBook")
    @ResponseBody
    public AjaxResult add(BookInfo bookInfo){
        bookInfoService.addBook(bookInfo);
        return AjaxResult.success("添加成功",null);
    }

    /**
     * 修改
     * @param bookInfo
     * @return
     */
    @PostMapping("/updateBook")
    @ResponseBody
    public AjaxResult updateBook(BookInfo bookInfo){
        bookInfoMapper.updateByPrimaryKey(bookInfo);
        return AjaxResult.success("添加成功",null);
    }



    /**
     * 批量删除
     * @param
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult deleteBook(String ids){

        if (ids.contains("-")){
            List<Long> list = new ArrayList<>();
            String[] bookIds = ids.split("-");
            for (String str : bookIds){
                list.add(Long.parseLong(str));
            }
            QueryWrapper<BookBorrow> bookBorrowQueryWrapper = new QueryWrapper<>();
            bookBorrowQueryWrapper.in("book_id", list).eq("real_date","0");
            List<BookBorrow> borrows = bookBorrowService.list(bookBorrowQueryWrapper);
            if (borrows != null && borrows.size() != 0){
                return AjaxResult.success("尚有图书借阅未归还，不能删除",null);
            }
            bookInfoMapper.deleteLatch(bookIds);
            return AjaxResult.success("批量删除成功",null);
        }
        //单个
        QueryWrapper<BookBorrow> bookBorrowQueryWrapper = new QueryWrapper<>();
        bookBorrowQueryWrapper.eq("book_id",ids).eq("real_date","0");
        List<BookBorrow> list = bookBorrowService.list(bookBorrowQueryWrapper);
        if (list != null && list.size() != 0){
            return AjaxResult.success("尚有图书借阅未归还，不能删除",null);
        }
        bookInfoMapper.deleteByPrimaryKey(Long.parseLong(ids));
        return AjaxResult.success("删除成功",null);

    }

}
